<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<script type="text/javascript" src="../../lib/commonUtil.js"></script>
<title>资源管理</title>
</head>
<script type="text/javascript">
/*Ext.Loader.setConfig({
    enabled: true
});
Ext.require([
    '*'
]);
*/
var parentId = 0;
    
Ext.onReady(function(){
    var leaderStore = Ext.create('Ext.data.Store', {
        fields: ['id', 'name'],  
        remoteSort: true, 
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/teacher/get_leaders',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        }
    });

    var addDeptWin;
    var addDeptForm = new Ext.FormPanel({
		id: 'addDeptForm',
		url:'/dept/add',
    	method:'POST',
    	frame:true,
    	style:'border-width:0 0 0 0;',
    	bodyBorder:false,
    	bodyPadding: '0 40 0 0',
    	layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
	    items: [
			{
                // xtype: 'textfield',
                // hidden: true,
                // name: 'department_id',
                // id: 'department_id'
                xtype: 'hidden',
                name: 'department_id',
                id: 'department_id'
            },{
                xtype:'textfield',
                fieldLabel:'院系名称',
                name:'department_name',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入院系名称',
                id:'department_name',
                width: 100
            },{
                xtype: "combobox",
                name: "leader_id",
                id: 'leader_id',
                fieldLabel: "主任名字",
                labelAlign: 'right',
                store: leaderStore,
                editable: true,
                displayField: "name",
                valueField: "id",
                emptyText: "--显示全部--",
                queryMode: "local",
                triggerAction: "all",
                selectOnFocus : true,
                typeAhead: true,
                typeAheadDelay: 1,
                forceSelection: true,
                allowBlank: false
            }
	    ],
	    buttons:[{ 
    		text:'保  存',
    		icon:'../../img/accept.png',
    		handler:function(){
    			if(!addDeptForm.getForm().isValid()){
    				 Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
    				 return;
                }
                addDeptForm.form.submit({
                	waitMsg:'正在保存数据...',
                	success:function(addDeptForm,action){
                		if(action.result.success){
                			Ext.Msg.alert('信息提示',action.result.msg,function(){
                                //store.reload();
                                Ext.getCmp('addDeptForm').getForm().reset();
                                addDeptWin.close();
                                refreshDeptTree('/dept/tree', tree);
                                //tree.expandAll();
                            });
                		} else {
                			Ext.Msg.alert('信息提示',action.result.msg);
                		}
                	},
                	failure:function(form,action){
                		Ext.Msg.alert('信息提示', action.result.msg);
                	}
                });
    		}
    	},{
    		text:'关  闭',
    		icon:'../../img/close.png',
    		handler:function(){
    			Ext.getCmp('addDeptForm').getForm().reset();
    			addDeptWin.close();
    		}
    	}]
    });
    
    var teacherStore = Ext.create('Ext.data.Store', {
        fields: ['id', 'name'],  
        remoteSort: true, 
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/teacher/get_teachers',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        }
    })

    var addClassWin;
    var addClassForm = new Ext.FormPanel({
        id: 'addClassForm',
		url:'/class/add',
    	method:'POST',
    	frame:true,
    	style:'border-width:0 0 0 0;',
    	bodyBorder:false,
    	bodyPadding: '0 40 0 0',
    	layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
	    items: [
			{
                // xtype:'textfield',
                // fieldLabel:'隐藏院系id',
                // name:'department_id',
                // labelAlign: 'right',
                // maxLength:35,
                // allowBlank:false,
                // tooltip:'',
                // id:'department_id',
                // width: 100,
                // hidden: true
                xtype: 'hidden',
                id: 'department_id',
                name: 'department_id'
            },{
                xtype: 'hidden',
                id: 'class_id',
                name: 'class_id'
            },{
                xtype:'textfield',
                fieldLabel:'班级名称',
                name:'class_name',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入班级名称',
                id:'class_name',
                width: 100
            },{
                xtype: "combobox",
                name: "teacher_id",
                id: 'teacher_id',
                fieldLabel: "老师名字",
                labelAlign: 'right',
                store: teacherStore,
                editable: true,
                displayField: "name",
                valueField: "id",
                emptyText: "--显示全部--",
                queryMode: "local",
                triggerAction: "all",
                selectOnFocus : true,
                typeAhead: true,
                typeAheadDelay: 1,
                forceSelection: true,
                editable: false,
                allowBlank: false
            }
	    ],
	    buttons:[{ 
    		text:'保  存',
    		icon:'../../img/accept.png',
    		handler:function(){
    			if(!addClassForm.getForm().isValid()){
    				 Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
    				 return;
                }
                addClassForm.form.submit({
                	waitMsg:'正在保存数据...',
                	success:function(addDeptForm,action){
                		if(action.result.success){
                			Ext.Msg.alert('信息提示',action.result.msg,function(){
                                //store.reload();
                                Ext.getCmp('addClassForm').getForm().reset();
                                addClassWin.close();
                                refreshDeptTree('/dept/tree', tree);
                                //tree.expandAll();
                            });
                		} else {
                			Ext.Msg.alert('信息提示',action.result.msg);
                		}
                	},
                	failure:function(form,action){
                		Ext.Msg.alert('信息提示', action.result.msg);
                	}
                });
    		}
    	},{
    		text:'关  闭',
    		icon:'../../img/close.png',
    		handler:function(){
    			Ext.getCmp('addClassForm').getForm().reset();
    			addClassWin.close();
    		}
    	}]
    });
    
    var treeStore = Ext.create('Ext.data.TreeStore', {
        fields: [
            {
                name:'dept_name',
                type:'string'
            },{
                name:'text',
                type:'string'
            }
        ],
        nodeParam:'parentId',
        proxy:{
            url:'/dept/tree',
            type:'ajax',
            reader:{
                type:'json'
            }
        }
    });
    
    menu_separator = new Ext.menu.Separator();
    //创建分组选择树
	var tree = Ext.create('Ext.tree.Panel', {
        autoWidth:true,
        autoHeight:true,
        rootVisible: true,
        //store: treeStore,
        listeners:{
            'itemcontextmenu':function(menutree, record, item, index, e){
                e.preventDefault();
                e.stopEvent();
                var lvl0_menu = new Ext.menu.Menu({
                    floating : true,
                    items: [
                        {
                            text: '添加院系',
                            handler: function(){
                                if(!addDeptWin){
                                    addDeptWin = Ext.create('Ext.window.Window', {
                                        title: '添加院系信息',
                                        width: 320,
                                        height: 200,
                                        constrainHeader: true,
                                        closeAction:'hide',
                                        layout: 'fit',
                                        items: addDeptForm
                                    });
                                }else{
                                    addDeptWin.setTitle('添加院系信息');
                                }
                                addDeptForm.form.reset();
                                addDeptForm.form.url = '/dept/add';
                                addDeptWin.show();
                            }
                        }
                    ]
                });
                
                var lvl1_menu = new Ext.menu.Menu({
                    floating : true,
                    items: [
                        {
                            text: '编辑院系',
                            handler: function(){
                                if(!addDeptWin){
                                    addDeptWin = Ext.create('Ext.window.Window', {
                                        title: '修改院系信息',
                                        width: 320,
                                        height: 200,
                                        constrainHeader: true,
                                        closeAction:'hide',
                                        layout: 'fit',
                                        items: addDeptForm
                                    });
                                }else{
                                    addDeptWin.setTitle('修改院系信息');
                                }
                                addDeptForm.form.reset();
                                addDeptForm.form.findField('department_id').setValue(record.get('id'));
                                addDeptForm.form.findField('department_name').setValue(record.raw.text);
                                addDeptForm.form.findField('leader_id').setValue(record.raw.director_id);
                                addDeptForm.form.url = '/dept/upd';
                                addDeptWin.show();
                            }
                        },{
                            text: '删除院系',
                            handler: function(){
                                if(!record.isLeaf() && record.firstChild){
                                    Ext.Msg.alert('警告','请先删除院系下所有班级');
                                    return;
                                }
                                Ext.Msg.confirm('系统提示','请确定删除该院系', function(btn){
                                    if(btn=='yes'){
                                        Ext.Ajax.request({
                                            url: '/dept/del/'+record.get('id'),
                                            method : 'POST',
                                            success: function(response, opts) {
                                                var data = eval("("+response.responseText+")");
                                                if(data.success){
                                                    Ext.Msg.alert('信息提示',data.msg,function(){
                                                        refreshDeptTree('/dept/tree', tree);
                                                        //tree.expandAll();
                                                    });
                                                } else {
                                                    Ext.Msg.alert('信息提示',data.msg);
                                                }
                                            },
                                            failure: function(response, opts) {
                                                var data = eval("("+response.responseText+")");
                                                if(!data.success){
                                                    Ext.Msg.alert('信息提示',data.msg);
                                                } else {
                                                    Ext.Msg.alert("信息提示","网络错误，请稍候再试");
                                                }
                                            }
                                        });
                                    }
                                });
                            }
                        },menu_separator,
                        {
                            text: '添加班级',
                            handler: function(){
                                if(!addClassWin){
                                    addClassWin = Ext.create('Ext.window.Window', {
                                        title: '添加班级信息',
                                        width: 320,
                                        height: 200,
                                        constrainHeader: true,
                                        closeAction:'hide',
                                        layout: 'fit',
                                        items: addClassForm
                                    });
                                }else{
                                    addClassWin.setTitle('添加班级信息');
                                }
                                // 为form里的department_id赋值
                                addClassForm.form.findField('department_id').setValue(record.get('id'));
                                addClassForm.form.url = '/class/add';
                                //console.log('addClassForm.form.department_id' + addClassForm.form.findField('department_id').getValue());
                                addClassWin.show();
                            }
                        }
                    ]
                });
                
                var lvl2_menu = new Ext.menu.Menu({
                    floating : true,
                    items: [
                        {
                            text: '编辑班级',
                            handler: function(){
                                if(!addClassWin){
                                    addClassWin = Ext.create('Ext.window.Window', {
                                        title: '修改班级信息',
                                        width: 320,
                                        height: 200,
                                        constrainHeader: true,
                                        closeAction:'hide',
                                        layout: 'fit',
                                        items: addClassForm
                                    });
                                }else{
                                    addClassWin.setTitle('修改班级信息');
                                }
                                // 为form里的department_id赋值
                                addClassForm.form.reset();
                                addClassForm.form.findField('class_id').setValue(record.get('id'));
                                addClassForm.form.findField('class_name').setValue(record.raw.text);
                                addClassForm.form.findField('teacher_id').setValue(record.raw.leader_id);
                                addClassForm.form.url = '/class/upd';
                                //addClassForm.form.findField('class_id').setValue(record.get('id'));
                                //console.log('addClassForm.form.department_id' + addClassForm.form.findField('department_id').getValue());
                                addClassWin.show();
                            }
                        },{
                            text: '删除班级',
                            handler: function(){
                                Ext.Msg.confirm('系统提示','请确定删除该班级', function(btn){
                                    if(btn=='yes'){
                                        Ext.Ajax.request({
                                            url: '/class/del/'+record.get('id'),
                                            method : 'POST',
                                            success: function(response, opts) {
                                                var data = eval("("+response.responseText+")");
                                                if(data.success){
                                                    Ext.Msg.alert('信息提示',data.msg,function(){
                                                        refreshDeptTree('/dept/tree', tree);
                                                        //tree.expandAll();
                                                    });
                                                } else {
                                                    Ext.Msg.alert('信息提示',data.msg);
                                                }
                                            },
                                            failure: function(response, opts) {
                                                var data = eval("("+response.responseText+")");
                                                if(!data.success){
                                                    Ext.Msg.alert('信息提示',data.msg);
                                                } else {
                                                    Ext.Msg.alert("信息提示","网络错误，请稍候再试");
                                                }
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    ]
                });

                var type = record.raw.node_type;
                console.log(record.raw.node_type);
                switch(type)
                {
                    case 'school':
                        lvl0_menu.showAt(e.getXY());
                        break;
                    case 'dept':
                        lvl1_menu.showAt(e.getXY());
                        break;
                    case 'class':
                        lvl2_menu.showAt(e.getXY());
                        break;
                    default:
                        lvl0_menu.showAt(e.getXY());
                        break;
                }
                
            }
        }
    });
    
    //创建全景视图端
    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'border',//框式布局
        items: [
            {
                region: 'center',
                stateId: 'navigation-panel',
                id: 'center-panel',
                border:false,
                title: '分组选择树结构',
                split: true,
                width: 250,
                collapsible: true,
                margins: '0 0 0 5',
                layout: 'fit',
                items: tree
            }
           ]
    });
    
    //初始化表格数据
	//queryStoreByCatId();
    //加载主任和老师列表
    leaderStore.load();
    teacherStore.load();

    refreshDeptTree('/dept/tree', tree);
    tree.expandAll();
});
</script>
<body>

</body>
</html>